// areaeditor.h : contains the window class to create and modify area files
//$Id: AreaEditor.h 47 2009-09-26 21:05:06Z brwarner2 $
#include "WindowFactory.h"

#pragma once

class CAreaEditor
{
public:
	CAreaEditor(void);
	~CAreaEditor(void);

	//Handles the messaging
	LRESULT Message( UINT, WPARAM, LPARAM );

	//Sets the window
	void SetHWND( HWND hWnd ) { m_hWindow = hWnd; }

	//Sends information to the window factory
	static void WindowInfo( WindowInfo& info );

	//Registers the child windows used by the control window
	static void RegisterClasses( void );

	//Loads an area file
	void LoadArea( const std::string& filename );

	//Saves an area file
	void SaveArea( const std::string& filename );

	//Position controls correctly
	BOOL SortObjects( HWND hWnd = 0 );

private:
	//HWND
	HWND m_hWindow;

	//Level preview window
	HWND m_hLevel;

	//Properties control
	HWND m_hProperties;
	const static unsigned int IDLV_PROP = 201;

	//Area information:
	//Background name
	std::string m_backgroundName;

	//Creates the child controls
	void CreateControls( void );

	//Object structure
	struct Object
	{
		typedef std::map<std::string, std::string> ParameterList;

		//List of properties
		ParameterList properties;

		//Script name
		std::string type;

		//Sprite for the object
		Gdiplus::Image* pSprite;

		//Hit rect for sprite
		RECT hitRect;
	};

	//Updates a hit rect with a new position
	static void UpdateHitRect( LPRECT pRect, unsigned int x, unsigned int y );

	//Gets the width of a sprite frame from an info file
	static unsigned int GetWidth( const std::string& filename );

	//Gets the height of a sprite frame from an info file
	static unsigned int GetHeight( const std::string& filename );

	//List of objects
	typedef std::vector<Object*> ObjectList;
	ObjectList m_objects;

	//Handle to the property edit window
	HWND m_hPropertyEdit;
	const static unsigned int IDE_PROPEDIT = 200;

	//Name of the item being edited
	std::string m_editItemName;

	//Selected object
	Object* m_pSelected;

private:
	//Level preview callback
	static LRESULT CALLBACK LevelPreviewCallback(HWND, UINT, WPARAM, LPARAM);

	//Sort parameter
	static bool SortPrep( Object*, Object* );

	//Custom messages for it
	const static unsigned int WM_SETSPRITE = (WM_USER+1);
	const static unsigned int WM_GIVEHANDLE = (WM_USER+2);
	const static unsigned int WM_SORTLIST = (WM_USER+3);

	const static unsigned int LVLN_SELECT = (WMN_LAST+2);
	struct NMHDSELECTINFO
	{
		NMHDR hdr;
		unsigned int objID;
	};

	//Level information
	struct LevelInfo
	{
		//Background sprite
		Gdiplus::Image *pSprite;

		//Object list
		ObjectList* pObjects;

		//Object being dragged
		Object* pDrag;

		//Drag offsets
		int offsetX, offsetY;

		//Handle to the right click menu
		HMENU hParentMenu;
		HMENU hClickMenu;
	};

	//Create object dialog box callback
	static BOOL CALLBACK CreateObjectDialogProc(HWND, UINT, WPARAM, LPARAM);

	//Loads the object list
	typedef std::map< std::string, std::list<std::string> > ObjectTypeList;
	static ObjectTypeList LoadObjectList( void );
};
